package ie.dkit.java3Demulation.gui;

import ie.dkit.java3Demulation.drawer.Drawable;
import ie.dkit.java3Demulation.objects.GraphicsObject;
import ie.dkit.java3Demulation.objects3d.Line3D;
import ie.dkit.java3Demulation.objects3d.Point3D;
import ie.dkit.java3Demulation.transforming.Transformable;
import ie.dkit.java3Demulation.transforming.TransformableGroup;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Frame;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JLabel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

/**
 * @author Sebastian Ruehl
 * 
 * This program is free software; you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation
 * 
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
 * details.
 * 
 * You should have received a copy of the GNU General Public License along with
 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
 * Place, Suite 330, Boston, MA 02111-1307 USA
 */
public class GUI extends Frame {

	/**
	 * 
	 */
	private static final long serialVersionUID = -1264907791956723129L;

	protected GraphicsObjectTableModel<GraphicsObject> graphicsObjectTableModel = new GraphicsObjectTableModel<GraphicsObject>();

	protected TransformableGroup transformables = new TransformableGroup();

	protected DisplayCanvas m_DisplayCanvas;

	private ControlPanel m_ControlPanel;

	private Line3D m_XAxis, m_YAxis, m_ZAxis;

	public GraphicsObject copyPaste;

	protected JLabel positionLabel;

	public GUI() {
		initialize();
	}

	/**
	 * This method initializes this
	 * 
	 */
	private void initialize() {
		this.setSize(640, 480);
		this.setLayout(new BorderLayout());
		add(getM_DisplayCanvas(), BorderLayout.CENTER);
		add(getM_ControlPanel(), BorderLayout.EAST);
		add(getPositionLable(), BorderLayout.SOUTH);
		addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent we) {
				System.exit(0);
			}
		});
		m_XAxis = new Line3D(new Point3D(-10000, 0, 0),
				new Point3D(10000, 0, 0));
		m_YAxis = new Line3D(new Point3D(0, -10000, 0),
				new Point3D(0, 10000, 0));
		m_ZAxis = new Line3D(new Point3D(0, 0, -10000),
				new Point3D(0, 0, 10000));
		this.setTitle("Java 3D Emulation");
		setVisible(true);
	}

	private JLabel getPositionLable() {
		if (positionLabel == null) {
			positionLabel = new JLabel("Position");
		}
		return positionLabel;
	}

	private DisplayCanvas getM_DisplayCanvas() {
		if (m_DisplayCanvas == null) {
			m_DisplayCanvas = new DisplayCanvas(this);
			m_DisplayCanvas.setBackground(Color.white);
		}
		return m_DisplayCanvas;
	}

	private ControlPanel getM_ControlPanel() {
		if (m_ControlPanel == null) {
			m_ControlPanel = new ControlPanel(this);
		}
		return m_ControlPanel;
	}

	public void showCoordinateSystem() {
		// Coordinate System
		m_DisplayCanvas.add(m_XAxis);
		m_DisplayCanvas.add(m_YAxis);
		m_DisplayCanvas.add(m_ZAxis);
	}

	public void hideCoordinateSystem() {
		m_DisplayCanvas.remove(m_XAxis);
		m_DisplayCanvas.remove(m_YAxis);
		m_DisplayCanvas.remove(m_ZAxis);
	}

	public void addGraphics(GraphicsObject graphicsObject) {
		graphicsObjectTableModel.add(graphicsObject);
		if (graphicsObject instanceof Transformable) {
			transformables.add((Transformable) graphicsObject);
		}
		if (graphicsObject instanceof Drawable) {
			m_DisplayCanvas.add((Drawable) graphicsObject);
		}
		graphicsObjectTableModel.fireTableDataChanged();
	}

	public void removeGraphics(GraphicsObject graphicsObject) {
		graphicsObjectTableModel.remove(graphicsObject);
		if (graphicsObject instanceof Transformable) {
			transformables
					.remove((Transformable) graphicsObject);
		}
		if (graphicsObject instanceof Drawable) {
			m_DisplayCanvas.remove((Drawable) graphicsObject);
		}
		graphicsObjectTableModel.fireTableDataChanged();
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		try {
			UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
			new GUI();
		} catch (ClassNotFoundException e1) {
			e1.printStackTrace();
		} catch (InstantiationException e1) {
			e1.printStackTrace();
		} catch (IllegalAccessException e1) {
			e1.printStackTrace();
		} catch (UnsupportedLookAndFeelException e1) {
			e1.printStackTrace();
		}
	}
} // @jve:decl-index=0:visual-constraint="10,10"
